package com.neuedu.yyzx.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.neuedu.yyzx.common.Ret;
import com.neuedu.yyzx.config.security.PreAuthorize;
import com.neuedu.yyzx.po.Menu;
import com.neuedu.yyzx.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 金山
 * 项目：yyzx
 * site: https://blog.fulfill.com.cn
 * 描述
 * @data 2025/3/2514:23
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Autowired
    private MenuService menuService;


    /**
     * 分页查询
     *
     * @param page
     * @param menu
     * @return
     */
    @PreAuthorize("persssion:menu:list") // 验证查询权限
    @GetMapping("/page")
    public Ret<List<Menu>> page(Page<Menu> page, Menu menu) {

        LambdaQueryWrapper<Menu> queryWrapper = new LambdaQueryWrapper<>();
        if (menu.getMenuName() != null) {
            queryWrapper.like(Menu::getMenuName, menu.getMenuName());   //  where  menu_name like  ?
        }

        // 排序 ORDER BY createtime DESC
        queryWrapper.orderByDesc(Menu::getCreateTime);

        // 分页查询
        page = menuService.page(page, queryWrapper);
        return Ret.ok(page);
    }


    /**
     * 用户的添加或者更新
     *
     * @param menu
     * @return
     */
    @PostMapping("/saveOrUpdate")
    public Ret<Boolean> saveOrUpdate(@RequestBody Menu menu) {
        boolean success = menuService.saveOrUpdate(menu);
        return Ret.ok(success);
    }


    /**
     * 根据主键查询
     *
     * @param id
     * @return
     */
    @GetMapping("/get/{id}")
    public Ret<Menu> get(@PathVariable int id) {
        Menu user = menuService.getById(id);
        return Ret.ok(user);
    }


    /**
     * 根据主键删除
     * <p>
     * /user/id
     * method delete
     *
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Ret<Boolean> removeById(@PathVariable int id) {
        return Ret.ok(menuService.removeById(id));
    }


    @PreAuthorize("permission:menu:list") // 验证查询权限
    @GetMapping("/getAllTree")
    public Ret<List<Menu>> getAllTree() {
        List<Menu> menuList = menuService.getAllTree();

        return Ret.ok(menuList);
    }


}
